package com.qf.controller;

import com.qf.pojo.Dept;
import com.qf.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping(value = "/depts")
    public List<Dept> getList () {
        List<Dept> depts = deptService.findAll ();
        return depts;
    }

    @DeleteMapping(value = "/dept{id}")
    public ResponseEntity<String> delete (@PathVariable("id") int id) {
        boolean bl = deptService.delete (id);
        if (bl) return new ResponseEntity<String> ("delete successfully", HttpStatus.OK);
        return new ResponseEntity<String> ("删除失败", HttpStatus.FORBIDDEN);
    }

    @GetMapping(value = "/dept{id}")
    public Dept findOne (@PathVariable("id") int id) {
        Dept one = deptService.findOne (id);
        System.out.println(123);
        return one;
    }

    @PutMapping("/dept")
    public ResponseEntity<String> update (@RequestBody Dept dept) {
        boolean bl = deptService.update (dept);
        if (bl) return new ResponseEntity<String> ("更新成功", HttpStatus.OK);
        return new ResponseEntity<String> ("更新失败", HttpStatus.FORBIDDEN);
    }

    @PostMapping("/dept")
    public Dept addDept (@RequestBody Dept dept) {
        Dept dept1 = deptService.addDept (dept);
        return dept1;
    }
}
